﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace JBrute
{
   class Commands
   {
      private Commands() { }// prevent compiler from generating a ctor
      public static RoutedCommand SetFocusTextCommand = new RoutedCommand();
      public static void ExecutedSetFocusTextCommand(object sender, ExecutedRoutedEventArgs e)
      {
         ((MainWindow)(System.Windows.Application.Current.MainWindow)).FindMeText.Focus();
      }
      public static RoutedCommand ChangeIMEModeCommand = new RoutedCommand();
      public static void ExecutedChangeIMEModeCommand(object sender, ExecutedRoutedEventArgs e)
      {
         switch (ViewModel.Instance.IMEMode)
         {
            case ViewModel.IMEModeType.Hirgana:
               ViewModel.Instance.IMEMode = ViewModel.IMEModeType.Katakana;
               break;
            case ViewModel.IMEModeType.Katakana:
               ViewModel.Instance.IMEMode = ViewModel.IMEModeType.Kanji;
               break;
            case ViewModel.IMEModeType.Kanji:
               ViewModel.Instance.IMEMode = ViewModel.IMEModeType.Off;
               break;
            case ViewModel.IMEModeType.Off:
               ViewModel.Instance.IMEMode = ViewModel.IMEModeType.Hirgana;
               break;
            case ViewModel.IMEModeType.Disabled:
               ViewModel.Instance.IMEMode = ViewModel.IMEModeType.Disabled;
               break;
         }
         System.Globalization.CultureInfo cultureinfo = null;
         if (ViewModel.IMEModeType.Kanji == ViewModel.Instance.IMEMode)
         {
            cultureinfo = System.Globalization.CultureInfo.CreateSpecificCulture("ja-JP");
            InputLanguageManager.Current.CurrentInputLanguage = cultureinfo;
            InputMethod.Current.ImeConversionMode = ImeConversionModeValues.FullShape
               | ImeConversionModeValues.Native;
            InputMethod.Current.ImeSentenceMode = ImeSentenceModeValues.PhrasePrediction;
            // Turn on ImeState last.  Some systems (Vista?) require this.
            InputMethod.Current.ImeState = InputMethodState.On;
         }
         else if (ViewModel.IMEModeType.Hirgana == ViewModel.Instance.IMEMode)
         {
            cultureinfo = System.Globalization.CultureInfo.CreateSpecificCulture("ja-JP");
            
            InputLanguageManager.Current.CurrentInputLanguage = cultureinfo;
            InputMethod.Current.ImeConversionMode = ImeConversionModeValues.FullShape
               | ImeConversionModeValues.Native;
            InputMethod.Current.ImeSentenceMode = ImeSentenceModeValues.None;
            InputMethod.Current.ImeState = InputMethodState.On;
         }
         else if (ViewModel.IMEModeType.Katakana == ViewModel.Instance.IMEMode)
         {
            cultureinfo = System.Globalization.CultureInfo.CreateSpecificCulture("ja-JP");
            InputLanguageManager.Current.CurrentInputLanguage = cultureinfo;
            InputMethod.Current.ImeConversionMode = ImeConversionModeValues.FullShape
               | ImeConversionModeValues.Native
               | ImeConversionModeValues.Katakana;
            InputMethod.Current.ImeSentenceMode = ImeSentenceModeValues.None;
            InputMethod.Current.ImeState = InputMethodState.On;
         }
         else if (ViewModel.IMEModeType.Off == ViewModel.Instance.IMEMode)
         {
            cultureinfo = System.Globalization.CultureInfo.InstalledUICulture;
            InputLanguageManager.Current.CurrentInputLanguage = cultureinfo;
            InputMethod.Current.ImeState = InputMethodState.Off;
         }
         else if (ViewModel.IMEModeType.Disabled== ViewModel.Instance.IMEMode)
         {
            // Nothing.
         }
         else
            throw new ArgumentException("ViewModel.Instance.IMEMode");
            
         ExecutedSetFocusTextCommand(sender, e);
      }
      // Synchronously extract every resultsitem from the underlying ItemsProvider.
      public static void CopyLVExecuted(object target, ExecutedRoutedEventArgs e)
      {
         string copyContent = String.Empty;
         Cursor curse = Mouse.OverrideCursor;
         Mouse.SetCursor(Cursors.Wait);
         int count = ViewModel.Instance.ResultsItemList.ItemsProvider.FetchCount();
         int overallCount;
         IList<ResultsItem> list = ViewModel.Instance.ResultsItemList.ItemsProvider.FetchRange(0, count, out overallCount);
         foreach (ResultsItem ri in list)
         {
            {
               copyContent += ri.kanji+ "\t";
               copyContent += ri.kana + "\t";
               copyContent += ri.english;
               copyContent += Environment.NewLine;
            }
         }
         Clipboard.SetText(copyContent);
         Mouse.SetCursor(curse);
      }
      public static void CanExecuteCommand(object sender, CanExecuteRoutedEventArgs e)
      {
         if (e.Source != null)
         {
            e.CanExecute = true;
         }
         else
         {
            e.CanExecute = false;
         }
      }
   }

}
